package logic

import (
	"context"
	"time"
	"weixin/moble/cache"
	"weixin/moble/mysql"

	"weixin/weixinrpc/internal/svc"
	"weixin/weixinrpc/weixinrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CircleShowLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCircleShowLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CircleShowLogic {
	return &CircleShowLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CircleShowLogic) CircleShow(in *weixinrpc.CircleShowRequest) (*weixinrpc.CircleShowResponse, error) {
	// todo: add your logic here and delete this line
	page := in.Page
	size := in.Size
	offset := (page - 1) * size
	var user []mysql.Circle
	mysql.DB.Debug().Where("user_id=?", in.UserId).Offset(int(offset)).Limit(int(size)).Find(&user)
	var circleList []*weixinrpc.CircleList
	for _, circle := range user {
		list := weixinrpc.CircleList{
			Title:  circle.Title,
			UserId: int64(circle.ID),
		}
		circleList = append(circleList, &list)
	}
	cache.Rdb.Set(l.ctx, "CircleShow", circleList, time.Minute*6)
	return &weixinrpc.CircleShowResponse{
		List: circleList,
	}, nil
}
